package com.google.code.gestion_de_conferences;


import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class RoomsListActivity extends ListActivity 
{
	AABDatabaseManager db;
	
	//this is to know if there is or not rooms to display so we display the context menu 
	Boolean rooms_exist = true; 
	

	/**
	 * This function is launched when the activity is launched
	 * 
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	 * 
	 */
	public void onCreate(Bundle icicle) 
	{
	    super.onCreate(icicle);
	    
	    rooms_exist =  this.displayRooms();
	    
	    //Get the list view
	    ListView list =  getListView();
	   	    
	    //register the context menu for the listview if there is rooms
	    if(rooms_exist)
	    {
	    	registerForContextMenu(list);
	    }
	}
	
	/**
	 * This function display the list of rooms 
	 * 
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com> 
	 * 
	 * @return items_exist 
	 */
	public boolean displayRooms()
	{
		   
        Room room = new Room();
	    db = new AABDatabaseManager(this);
	  	boolean items_exist = true;
	  
	  	ArrayList<ArrayList<Object>> allRoomsArrays = new ArrayList<ArrayList<Object>>();
	    
	    //Get all rooms in the db
	    allRoomsArrays = db.getTablesRowsAsArrays (room);
	    
	    //array that will hold user data to be displayed
	    String[] values;
	   	
	    //check if we don't have any rooms
		if(allRoomsArrays.size()==0)
		{
			values = new String[1];
    		values[0] = "No items to display";  
    		items_exist = false;
		}  
		else
		{
			values = new String[allRoomsArrays.size()];			
		}
		
    	for (int position=0; position < allRoomsArrays.size(); position++)
    	{
    		ArrayList<Object> row = allRoomsArrays.get(position);    		 			
    	    			
    		//display full name - capacity
    		values[position] = row.get(1).toString()+"-"+row.get(3).toString();
    			    			
    	} 
    	
	    // Use your own layout
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.room_row_layout, R.id.label, values);
	    setListAdapter(adapter);
	    
	    return items_exist;
	}
	/**
	 * This function allow to create the context menu when clicking on a user
	 * 
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	 * 
	 * @param ContextMenu menu
	 * @param View v
	 * @param ContextMenuInfo menuInfo
	 */
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
	{
		 super.onCreateContextMenu(menu, v, menuInfo);
		 
		 MenuInflater inflater = getMenuInflater();
	     inflater.inflate(R.menu.room_context_menu, menu);              
	     		  
	}
	
	/**
	 * This function allows to set the action for the selected menu item
	 * 
	 * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	 * 
	 * @param MenuItem item
	 * @return boolean
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		//Get the position of the selected item
		AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		Intent i;
		
	    switch (item.getItemId())	 
	    {
		      case R.id.edit:     
		    	  i = new Intent(getApplicationContext(), AddroomActivity.class);
		    	 	    	  
		    	  //pass the position as variable to the AddroomActivity
		    	  i.putExtra("selectedRoomPosition",info.position);		    	  
		    	  startActivity(i);
		    	  break;	
		    	  //Rayan Zahab Reserve 
		      case R.id.reserve:     
		    	  i = new Intent(getApplicationContext(), ReservationActivity.class);
		    	 	    	  
		    	  Room res = new Room();    				    	
			      //get the selected room data
	  			  ArrayList<Object> dbRoomRes = db.getObjectFromPosition(info.position, res);
	  			  
	  			  int id = Integer.parseInt(dbRoomRes.get(0).toString());
		    	  i.putExtra("selectedRoomPosition",id);	
		    	  startActivity(i);
		    	  break;		  
		      case R.id.delete:     
		      	
		    	  Room r = new Room();    	
			    	
			      //get the selected room data
	  			  ArrayList<Object> dbRoom = db.getObjectFromPosition(info.position, r);
	  				
	  			  //set the room id to edit
	  			  r.setId(Integer.parseInt(dbRoom.get(0).toString()));
	  				
	  			  //delete room from db
	  			  db.deleteRow (r);
	  				
	  			  //redirect to rooms list
	  			  i = new Intent(getApplicationContext(), RoomsListActivity.class);
	    	      startActivity(i);
	    	      
	    	      Toast.makeText(this, "Conference room successfully deleted", Toast.LENGTH_LONG).show();
    	          break;
    	          
		    }

	  return true;
	}
	
	 /**
	  * This function opens the room info activity
	  * @author Hyame Alameddine <hyame.alameddine@gmail.com>
	  *  
	  * @param l listview
	  * @param v view
	  * @param position int
	  * @param id long
	  */
	 @Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		if (rooms_exist)
		{
			Intent i = new Intent(getApplicationContext(), RoomInfoActivity.class);
			i.putExtra("position", position);
	     	startActivity(i);
		}
		 
	}
	 
	 /**
	  * This function create the menu options 
	  * @author Hyame Alameddine <hyame.alameddine@gmail.com> 
	  * @param menu
	  */
	  @Override
	 public boolean onCreateOptionsMenu(Menu menu)
	 {
	     MenuInflater inflater = getMenuInflater();
	     inflater.inflate(R.menu.rooms_list_menu, menu);              
	     return true;
     }
	  
    /**
      * This function handles clicking any of the menu's items
	  * @author Hyame Alameddine <hyame.alameddine@gmail.com> 
	  * @param menu
	  */
	  @Override
	  public boolean onOptionsItemSelected(MenuItem item) 
	  {	  
		  Intent i;
		  
		  switch (item.getItemId()) 
		  {
	           
	          case R.id.add:     
	            	i = new Intent(getApplicationContext(), AddroomActivity.class);
	            	startActivity(i);
	                break;
	          case R.id.reservations:     
	            	i = new Intent(getApplicationContext(), ReservationsListActivity.class);
	            	startActivity(i);
	                break;
	          case R.id.users:     
	            	i = new Intent(getApplicationContext(), UsersListActivity.class);
	            	startActivity(i);
	                break;
	          case R.id.edit_profile: 	        	  
	            	i = new Intent(getApplicationContext(), RegisterActivity.class);
	            	i.putExtra("editAdmin", 1);
	            	startActivity(i);
	                break;
	          case R.id.logout: 
	            	i = new Intent(getApplicationContext(), LoginActivity.class);
	            	startActivity(i);
	                break;
	          case R.id.help:     
	                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	                alertDialog.setTitle("Help!");
	                alertDialog.setMessage("Gestion de conference is an application helps you manage reservations, add rooms ,add users and reserve rooms to organize conferences !");
	                alertDialog.setIcon(R.drawable.icon);
	                alertDialog.show();
	                break;
	          case R.id.developers: 
	            	Toast.makeText(this, "Rayan,Hyam,Mostafa!", Toast.LENGTH_LONG).show();
	                break;
	        
	        }
	        return true;
	    }
	    
} 

